package Entity;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Persistable {
    private String tableName;
    
    public Persistable(String tableName){
        this.tableName = tableName;
    }
    
    public String getSelectQuery(ArrayList<String> conditions){
        String result = "SELECT * FROM "+this.tableName+" WHERE ";
        
        for(int i=0; i<conditions.size();i++){
            result += conditions.get(i);
            if(i<conditions.size()-1){
                result += "AND";
            }
        }
        
        return result;
    }
    
    public String getInsertQuery(){
        String result = "INSERT INTO "+this.tableName;
        String column = "";
        String value = "";
        
        Class<?> c = this.getClass();
        Field[] fields = c.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            if(field.getType().getName().equals("java.util.Date")){
                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
                    Date d = (Date) field.get(this);
                    result = formatter.format(d);
                    
                    System.out.print(result+" ");
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(Persistable.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Persistable.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
        
        return result;
    }
}